package com.hospital.ecard.task;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.hospital.common.core.exception.ExceptionPrint;
import com.hospital.common.core.module.entity.ecard.RemoteUrl;
import com.hospital.common.core.result.Tip;
import com.hospital.common.core.utils.DateUtil;
import com.hospital.common.core.utils.RedisUtils;
import com.hospital.common.core.utils.SpringContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Calendar;

/**
 * @author lvyao
 * @description 异步执行获取电子健康卡的密钥按需要更新redis密钥
 * @date 2022/3/4
 */
@Slf4j
public class SetWorkKeyScheduled {
    private static Object object = new Object();
    @Autowired
    private volatile static RemoteUrl remoteUrl = SpringContextHolder.getBean(RemoteUrl.class);

    //设置到当天24点之间的失效时间

    public static String setWorkKey() {
        String startTime = DateUtil.getAllTime();
        boolean workKey = RedisUtils.isExistsKey("workKey");
        String authKeyStr = null;

        if (workKey&&RedisUtils.getKey("workKey") !=null) {
            authKeyStr = RedisUtils.getKey("workKey");
        } else {
            synchronized (object.getClass()) {
                try {
                    String body = "{\t\n" +
                            "  \"accessId\": \"" + remoteUrl.getAccessId() + "\"\n" +
                            "}";
                    String newUrl = remoteUrl.getNewUrl("0");

                    HttpResponse execute = HttpRequest.post(newUrl).body(body).execute();
                    Tip resultAll = JSON.parseObject(execute.body(), Tip.class);
                    if (resultAll != null) {
                        if (resultAll.getCode()==10000) {
                            Object data = resultAll.getData();
                            String key = JSON.parseObject(data.toString()).getString("key");
                            RedisUtils.setKey("workKey", key);
                            Long secondsTobeforedawn = getSecondsTobeforedawn();
                            RedisUtils.expire("workKey", Math.toIntExact(secondsTobeforedawn));
                            authKeyStr = key;
                        } else {
                            throw new RuntimeException(resultAll.getMessage());
                        }
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
        ExceptionPrint.printCommString(authKeyStr,startTime,DateUtil.getAllTime());
        return authKeyStr;
    }

    public static Long getSecondsTobeforedawn() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_YEAR, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 30);
        cal.set(Calendar.MILLISECOND, 0);
        return (cal.getTimeInMillis() - System.currentTimeMillis()) / 1000;
    }
}
